# -*- coding: utf-8 -*-

# Created on 06/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame

from core import Control

QUIT = 0
RUNNING = 1
FINISHED = 2
PAUSED = 3

class Game:
    """ Main class. It manages all the logic of the game and creates
    the scene where objects are displayed. Must be subclassed.
    
    * Methods:
    __init__(self, scene, controlManager, actorManager, collisionManager)
    initialize(self)
    main(self)
    __readInterruptions(self)
    _executeMainTasks(self)
    _executeInterruption(self, interruption)
    _checkEndConditions(self)
    _endGame(self)
    _onEndAction(self)
    
    * Member variables:
    controlManager -- ControlManager object
    scene -- Scene object
    state -- Current state of the game
    clock -- pygame.time.Clock() object
    actors -- Actors list
    colliders -- Colliders list
    scores -- Scores list
    scoreboards -- Scoreboards list
    teams -- Teams list
    players -- Players list
    
    """

    def __init__(self, scene, controlManager):
        """ Constructor. Inits pygame.
        
        * Arguments:
        scene -- Scene object
        controlManager -- ControlManager object
        
        """
        pygame.init()
        self.controlManager = controlManager
        self.scene = scene
        self.state = RUNNING
        self.clock = pygame.time.Clock()
        self.actors = []
        self.colliders = []
        self.scores = []
        self.scoreboards = []
        self.teams = []
        self.players = []

    def initialize(self):
        """ Initializes the game: initializes managers and scene.
        
        """
        self.controlManager.initialize()
        self.scene.initialize()

    def main(self):
        """ Main method.
        
        """
        # Main loop
        while self.state == RUNNING or self.state == PAUSED:
            self.clock.tick(60)
            if self.state != PAUSED:
                self._executeMainTasks()

                if self.state == FINISHED:
                    self._endGame()
                    break;

        pygame.quit()

    def __update(self):
        """ Updates some lists.
        
        """
        self.__updateListObjects(self.actors)
        self.__updateListObjects(self.colliders)
        self.__updateListObjects(self.scores)
        
    def __updateListObjects(self, list):
        """ Updates all the objects in a list.
        
        """
        for e in list:
            e.update()

    def __readInterruptions(self):
        """ Reads and executes interruptions from controlManager.
        
        """
        [self._executeInterruption(i) for i in self.controlManager.interruptions]

    def _executeInterruption(self, interruption):
        """ Executes an action related to an interruption.
        
        * Arguments:
        interruption -- Interruption to be executed
        
        """
        if interruption.name == "Quit":
            self.state = QUIT

    def _executeMainTasks(self):
        """ Executes the main tasks from the main loop:
        - Updates the control manager.
        - Reads interruptions.
        - Updates the lists of objects.
        - Updates and draws the scene.
        - Checks the end conditions. 
        
        """
        #    First, update the control manager to check if there is any new
        # action.
        self.controlManager.update()
        self.__readInterruptions()
        self.__update()
        self.scene.update()
        self.scene.draw()
        self._checkEndConditions()

    def _checkEndConditions(self):
        """ Checks the end conditions for the game to be finished.
        
        """
        pass
    
    def _endGame(self):
        """ Do the actions when the game ends.
        
        """
        print "Game over"
        pygame.time.wait(2000)
        self._onEndAction()
    
    def _onEndAction(self):
        """ Action to be executed when the game ends (i.e: Show a message; play a sound; etc.).
        
        """
        pass

# eof
